import requests
import execjs

# import time

# 获取js执行环境
js_code = ''
with open(r'sign.js', 'r', encoding='utf-8') as f:
    js_code = f.read()
js_compile = execjs.compile(js_code)


def get_token():
    # # 需要先 *1000 解决Python中和js中的时间戳不一致问题
    # request_time = int(time.time()) * 1000 + 100000000
    # # 这里需要转字符串，数字和字符串取的md5结果是不一样的
    # md5 = js_compile.call("hex_md5", str(request_time))
    # # 需要转成int类型，直接除是浮点数，有一位小数的
    # return '{}丨{}'.format(md5, int(request_time / 1000))
    return js_compile.call("get_token")


def get_data(page, m):
    url = f'https://match.yuanrenxue.com/api/match/1?page={page}&m={m}'
    headers = {
        "User-Agent": "yuanrenxue.project",
        "Cookie": "sessionid=pwin2ge9j7t9ttj5m15pjwar9m4sj9tc;",
    }
    return requests.get(url, headers=headers).json()


def main():
    value_list = []
    for page in range(1, 6):
        m = get_token()
        res = get_data(page, m)
        value_list += [v['value'] for v in res['data']]
    print(value_list)
    print(sum(value_list) / len(value_list))


if __name__ == '__main__':
    main()
